package com.yanqu.road.server.cmd.marriage;

import com.yanqu.road.entity.marriage.MarriageInfo;
import com.yanqu.road.logic.pb.MarriagePb;
import com.yanqu.road.pb.marriage.CrossMarriageProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.marriage.CrossUnionMarriageMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.C_PUBLISH_MARRIAGE_UNION, desc = "发布跨服商会联姻")
public class CrossUnionMarriagePublishCmd extends CrossCommand {
    private long userId;
    private MarriageInfo marriageInfo;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossMarriageProto.CrossMarriageApplyPublishUnionReqMsg reqMsg = CrossMarriageProto.CrossMarriageApplyPublishUnionReqMsg.parseFrom(packet.getBytes());
        marriageInfo = MarriagePb.parseMarriageInfo(reqMsg.getApplyData());
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        long publishTime = System.currentTimeMillis() / 1000;
        marriageInfo.setPublishTime(publishTime);
        ret = CrossUnionMarriageMgr.publishUnionMarriage(userId, marriageInfo);
        CrossMarriageProto.CrossMarriageApplyPublishUnionRespMsg.Builder respMsg = CrossMarriageProto.CrossMarriageApplyPublishUnionRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setChildUid(marriageInfo.getChildUid());
        respMsg.setPublishTime(publishTime);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_MARRIAGE_PUBLISH_UNION_RESP, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);
    }

    @Override
    public void sendData() {

    }
}
